//
//  UIBarButtonItem+Extension.swift
//  weibo
//
//  Created by sanya on 15/6/29.
//  Copyright © 2015年 sanya. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    // convenience 方便的
    convenience init(imageName:String,highlightedImageName:String?){
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        // ?? 表示如果前面的内容为nil, 使用后面的内容
        let himageName = highlightedImageName ?? imageName + "_highlighted"
        btn.setImage(UIImage(named: himageName), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        self.init(customView:btn)
    }
    ///  创建 UIBarButtonItem
    ///
    ///  :param: imageName            图像名
    ///  :param: highlightedImageName 高亮名，可以为 nil
    ///  :param: target               target
    ///  :param: actionName           actionName
    convenience init(imageName:String,highlightedImageName:String?,target:AnyObject?,actionName:String?) {
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        let himageName = highlightedImageName ?? imageName + "_highlighted"
        btn.setImage(UIImage(named: himageName), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        // 添加监听方法
        if actionName !=  nil {
            btn.addTarget(target, action: Selector(actionName!), forControlEvents: UIControlEvents.TouchUpInside)
        }
        self.init(customView: btn)
    }
}




